package com.fulihui.systemcore.repository.impl;

import com.fulihui.systemcore.dal.convertor.SysColumnConv;
import com.fulihui.systemcore.dal.dataobj.SysColumn;
import com.fulihui.systemcore.dal.dataobj.SysColumnExample;
import com.fulihui.systemcore.dal.mapper.SysColumnMapper;
import com.fulihui.systemcore.dto.SysColumnDTO;
import com.fulihui.systemcore.repository.SysColumnRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.inject.Inject;
import java.util.List;

/**
 * @author ssm on 2018/02/26 .
 */
@Repository
public class SysColumnRepositoryImpl implements SysColumnRepository {

    @Autowired
    private SysColumnMapper sysColumnMapper;


    private final transient Logger LOG = LoggerFactory.getLogger(getClass());

    @Override public SysColumnDTO queryById(Long id) {
        SysColumn sysColumn = sysColumnMapper.selectByPrimaryKey(id);
        LOG.info("sysColumn:{}",sysColumn.toString());
        return SysColumnConv.convert2DTO(sysColumn);
    }


    @Override public List<SysColumnDTO> query(SysColumnExample example) {
        List<SysColumn> sysColumns = sysColumnMapper.selectByExample(example);
        return SysColumnConv.convert2List(sysColumns);
    }

    public List<SysColumnDTO> query(SysColumn sysColumn) {
        LOG.info("sysColumn请求参数:{}",sysColumn.toString());
        List<SysColumn> sysColumns = sysColumnMapper.query(sysColumn);
        LOG.info("sysColumn list:{}",sysColumns.size());
        return SysColumnConv.convert2List(sysColumns);
    }

    @Override public void insert(SysColumnDTO dto) {
        sysColumnMapper.insertSelective(SysColumnConv.convert2DO(dto));
    }

    @Override public boolean updateValue(SysColumnDTO dto) {
        SysColumn sysColumn = SysColumnConv.convert2DO(dto);
        LOG.info("sysColumn:{}",sysColumn.toString());
        int update = sysColumnMapper.updateByPrimaryKeySelective(sysColumn);
        return update > 0 ? true:false;
    }

    @Override public boolean delete(Long id) {
        int delete = sysColumnMapper.deleteByPrimaryKey(id);
        return delete > 0?true:false;
    }

    @Override public boolean deleteByParentId(Long parentId) {
        SysColumnExample example = new SysColumnExample();
        SysColumnExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        int update = sysColumnMapper.deleteByExample(example);
        return update > 0 ? true :false;
    }

    @Override public int queryPageCount(SysColumnExample example) {
        return sysColumnMapper.countByExample(example);
    }
}
